package com.ruoyi.vote.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.topic.domain.HouseVoteTopic;
import com.ruoyi.topic.mapper.HouseVoteTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.vote.mapper.HouseVoteTMapper;
import com.ruoyi.vote.domain.HouseVoteT;
import com.ruoyi.vote.service.IHouseVoteTService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 投票项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-07
 */
@Service
public class HouseVoteTServiceImpl implements IHouseVoteTService 
{
    @Autowired
    private HouseVoteTMapper houseVoteTMapper;

    @Autowired
    private HouseVoteTopicMapper houseVoteTopicMapper;

    /**
     * 查询投票项目
     * 
     * @param voteId 投票项目主键
     * @return 投票项目
     */
    @Override
    public HouseVoteT selectHouseVoteTByVoteId(Long voteId)
    {
        return houseVoteTMapper.selectHouseVoteTByVoteId(voteId);
    }

    /**
     * 查询投票项目列表
     * 
     * @param houseVoteT 投票项目
     * @return 投票项目
     */
    @Override
    public List<HouseVoteT> selectHouseVoteTList(HouseVoteT houseVoteT)
    {
        return houseVoteTMapper.selectHouseVoteTList(houseVoteT);
    }

    /**
     * 新增投票项目
     * 
     * @param houseVoteT 投票项目
     * @return 结果
     */
    @Override
    public int insertHouseVoteT(HouseVoteT houseVoteT)
    {
        return houseVoteTMapper.insertHouseVoteT(houseVoteT);
    }

    /**
     * 修改投票项目
     * 
     * @param houseVoteT 投票项目
     * @return 结果
     */
    @Override
    public int updateHouseVoteT(HouseVoteT houseVoteT)
    {
        HouseVoteT houseVoteTVO = new HouseVoteT();
        houseVoteTVO.setVoteSts("1");
        List<HouseVoteT> houseVoteList = houseVoteTMapper.selectHouseVoteTList(houseVoteTVO);
        if(houseVoteList.size()>0){
            if(!houseVoteT.getVoteId().equals(houseVoteList.get(0).getVoteId())){
                throw new ServiceException("已有开启的项目");
            }
        }
        HouseVoteTopic hvt = houseVoteTopicMapper.selectHouseVoteProjectByVoteId(houseVoteT.getVoteId());
        if(hvt.getVoteScore()!=100){
            throw new ServiceException("题目分数不等于100不允许启动");
        }
        return houseVoteTMapper.updateHouseVoteT(houseVoteT);
    }

    /**
     * 批量删除投票项目
     * 
     * @param voteIds 需要删除的投票项目主键
     * @return 结果
     */
    @Override
    public int deleteHouseVoteTByVoteIds(Long[] voteIds)
    {
        return houseVoteTMapper.deleteHouseVoteTByVoteIds(voteIds);
    }

    @Transactional
    @Override
    public int updateVoteStatistic(Long[] voteIds)
    {
        HouseVoteT houseVoteTVO = new HouseVoteT();
        houseVoteTVO.setVoteSts("1");
        List<HouseVoteT> houseVoteList = houseVoteTMapper.selectHouseVoteTList(houseVoteTVO);
        if(houseVoteList.size()>0){
            for(Long voteId:voteIds){
                if(voteId.equals(houseVoteList.get(0).getVoteId())){
                    throw new ServiceException("项目已开启不允许统计");
                }
            }
        }
        for(Long voteId:voteIds){
            List<HouseVoteT> houseVoteTList = houseVoteTMapper.selectHouseVoteStatisticTByVoteId(voteId);
            if(houseVoteTList.size()>0){
                throw new ServiceException("项目不允许重复统计");
            }else{
                HouseVoteT houseVoteTVo = new HouseVoteT();
                houseVoteTVo.setVoteResult("1");
                houseVoteTVo.setVoteId(voteId);
                houseVoteTMapper.insertVoteStatistic(voteId);
                houseVoteTMapper.updateHouseVoteT(houseVoteTVo);
            }
        }
        return 1;
    }

    /**
     * 删除投票项目信息
     * 
     * @param voteId 投票项目主键
     * @return 结果
     */
    @Override
    public int deleteHouseVoteTByVoteId(Long voteId)
    {
        return houseVoteTMapper.deleteHouseVoteTByVoteId(voteId);
    }
}
